import constans from '../../../../assets/utils/Constans.js';
import DateUtil from '../../../../assets/utils/DateUtil.js';
import StringUtil from '../../../../assets/utils/StringUtil.js';

export default {
  created: function () {
    var self = this;
    self.selectProtocolTemplate(self);
    self.getSaleRentRecord(self)
  },
  data(){
    return {
      actionUrl: constans.uploadTemplate,
      tableData : [],
      textRecord : '',
      loading : false,
      uploadUrl : constans.uploadTemplate,

      fileList : []
    }
  },
  methods: {
    handleChange (file, fileList) {
      // if (this.loading == true) {
      //   this.loading = false;
      // } else {
      //   this.loading = true;
      // }
    },
    handleSuccess(response, file, fileList) {
      if (response.messageCode != null && response.messageCode === 1000) {
        this.showToast(response.message, 2, 1500);
        this.$router.push("/login");
        return false;
      }
      if (response.messageCode === 200) {
        this.$message({
          type: 'success',
          message: "上传成功"
        });
        this.selectProtocolTemplate(this);
      } else {
        this.$message({
          type: 'info',
          message: response.message
        });
      }
    },
    beforeAvatarUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf('.')+1)
      const extension = testmsg === 'doc'
      const extension2 = testmsg === 'docx'
      const isLt2M = file.size / 1024 / 1024 < 15
      if(!extension && !extension2) {
        this.$message({
          message: '上传文件只能是 doc、docx格式!',
          type: 'warning'
        });
      }
      if(!isLt2M) {
        this.$message({
          message: '上传文件大小不能超过 15MB!',
          type: 'warning'
        });
      }
      if (extension || extension2 && isLt2M) {
        this.$store.commit('LOADING_TOGGLE', true);
      }
      return extension || extension2 && isLt2M
    },
    handleError(err, file, fileList) {
      this.$message.error("服务器已关闭！");
    },
    formatUrl (row, column, cellValue) {
      if (cellValue == null || cellValue == '') {
        return "";
      }
      return cellValue.substring(cellValue.lastIndexOf("/") + 1, cellValue.length);
    },
    selectProtocolTemplate (self) {
      self.$http.get(constans.selectProtocolTemplate).then((response) => {
        var result = response.body;
        self.tableData = result.data;
        console.log(self.tableData);
      },(response) => {
        self.showToast("服务器已关闭！");
      });
    },
    uploadTemplate (tempCode, file) {

    },
    deleteTemplate (tempCode) {
      var self = this;
      this.$confirm('确认删除文件?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.$http.post(constans.deleteTemplate + "?tempCode=" + tempCode).then((response) => {
          var result = response.data;
          if (result.messageCode === 200) {
            this.$message({
              type: 'success',
              message: '删除成功'
            });
            self.selectProtocolTemplate(self);
          } else {
            self.showToast(result.message, 2, 1500);
          }
        },(response) => {
          self.showToast("服务器已关闭！");
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    pushSaleRentRecord () {
      var self = this;
      this.$confirm('确认重新发布租售说明?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.$http.post(constans.pushSaleRentRecord, {textContent : self.textRecord}).then((response) => {
          var result = response.data;
          if (result.messageCode === 200) {
            this.$message({
              type: 'success',
              message: '发布成功'
            });
            self.getSaleRentRecord(self);
          } else {
            self.showToast(result.message, 2, 1500);
          }
        },(response) => {
          self.showToast("服务器已关闭！");
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    getSaleRentRecord (self) {
      self.$http.get(constans.getSaleRentRecord).then((response) => {
        var result = response.data;
        self.textRecord = result.data;
      },(response) => {
        self.showToast("服务器已关闭！");
      });
    }
  }
}
